<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

Route::group(['method' => 'get', 'ext' =>'jpg|jpeg|gif|png'],function(Request $request){
    $url = $request::path();
    preg_match('/(.*?[.][a-zA-Z\d]{2,7})[!](\d+)x(\d+)x?(\d)?/',$url,$imageData);
    if(!empty($imageData)){
        $path  = $imageData[1];
        $width  = $imageData[2];
        $height = $imageData[3];
        if (empty($imageData[4])){
            $type = \think\Image::THUMB_SCALING;
        } else {
            $type = (int)$imageData[4];
        }
        try{
            if (!file_exists('./'.$path)){
               throw new \Exception('图片不存在');
            }
            $image = \think\Image::open($path);
            $image->thumb($width, $height, $type)->save('./'.$url);
            header("Content-type:".$image->mime());
            echo fread(fopen('./'.$url,'rb'),filesize('./'.$url));
            exit();
        }catch (\Exception $e){
            header("HTTP/1.0 404 Not Found");
            exit($e->getMessage());
        }
    }
});